
goog.provide('esn.FriendChip');

goog.require('esn.dao.FriendSurveyDao');
goog.require('esn.model.FriendSurveyModel');
goog.require('esn.soy.friendChip');
goog.require('goog.style');


/**
 * @param {esn.model.StudentModel|esn.model.FriendSurveyModel} model
 * @constructor
 */
esn.FriendChip = function(model, opt_surveyCient, opt_domHelper) {
  goog.base(this, opt_domHelper);

  /**
   * @type {esn.model.FriendSurveyModel}
   * @private
   */
  this.model_ = model instanceof esn.model.StudentModel ?
      new esn.model.FriendSurveyModel(model) : model;

  this.surveyClient_ = opt_surveyCient || null;

  // Set the ID of this object to the friend ID in the model.
  this.setId(this.model_.getFriend().getId());

  this.removeIconVisible_ = true;
};
goog.inherits(esn.FriendChip, goog.ui.Component);

esn.FriendChip.EventType = {
  REMOVE_CLICK: goog.events.getUniqueId('remove_click')
};

esn.FriendChip.prototype.removeIcon_;

esn.FriendChip.prototype.surveyDoa_;

esn.FriendChip.prototype.createDom = function() {
  goog.base(this, 'createDom');

  var tmplData = {
    Name: this.model_.getFriend().getFullName(),
    PhotoSrc: '/static/images/default-friend-photo.gif',
    RemoveIconSrc: '/static/images/delete.png'
  };
  this.setElementInternal(
      soy.renderAsElement(esn.soy.friendChip.content, tmplData));

  this.removeIcon_ = this.getElementByClass('removeIcon');

  if (!this.removeIconVisible_) {
    goog.style.showElement(this.removeIcon_, false);
  }
};

esn.FriendChip.prototype.enterDocument = function() {
  goog.base(this, 'enterDocument');

  this.getHandler().listen(
      this.removeIcon_,
      goog.events.EventType.CLICK,
      this.handleRemoveClick_);
};

esn.FriendChip.prototype.setRemoveIconVisible = function(visible) {
  this.removeIconVisible_ = visible;
  if (this.getElement()) {
    goog.style.showElement(this.removeIcon_, visible);
  }
};

/**
 * @return {esn.model.StudentModel}
 */
esn.FriendChip.prototype.getModel = function() {
  return this.model_.getFriend();
};

/**
 * @return {esn.model.FriendSurveyModel}
 */
esn.FriendChip.prototype.getSurvey = function() {
  return this.model_;
};

/**
 * @return {esn.dao.FriendSurveyDao}
 */
esn.FriendChip.prototype.getSurveyDoa = function() {
  if (!this.surveyClient_) {
    throw new Error('Cannot create friend survey doa without a survey client.');
  }

  if (!this.surveyDoa_) {
    this.surveyDoa_ = new esn.dao.FriendSurveyDao(this.model_,
        this.surveyClient_);
  }
  return this.surveyDoa_;
};

esn.FriendChip.prototype.handleRemoveClick_ = function(e) {
  this.dispatchEvent(esn.FriendChip.EventType.REMOVE_CLICK);
};
